package com.seeyon.apps.brwh.kit;

import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.util.JDBCAgent;
import org.apache.commons.logging.Log;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 直接操作数据库的类
 * </pre>
 * @date 2018年11月8日 上午11:10:15
 * @Copyright(c) Beijing Seeyon Software Co.,LTD
 */
public class DBKit {
    private static final Log log = CtpLogFactory.getLog(DBKit.class);
    @SuppressWarnings("unchecked")
    public static List<Map<String, Object>> excuteSQL(String sql, List<Object> params) throws Exception {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        JDBCAgent jdbc = new JDBCAgent(true);
        try {
            if(null == params) {
                jdbc.execute(sql);
            } else {
                jdbc.execute(sql, params);
            }
            if(!sql.startsWith("update")) {
                list = jdbc.resultSetToList();
            }
            
        } catch(Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            jdbc.close();
        }
        return list;
    }
    
    public static int updateOrDelData(String sql) throws BusinessException {
    	JDBCAgent jdbc = new JDBCAgent(true);
    	int num = 0;
    	try {
    		num = jdbc.execute(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
            jdbc.close();
        }
		return num;
    }

    public static List<Map<String, Object>> getDataBae(String selectSql) {
        List<Map<String, Object>> maps = new ArrayList<>();
        try {
            maps = DBKit.excuteSQL(selectSql.toString(), null);
        } catch (Exception e) {
            log.info("调用数据库获取数据：", e);
        }
        return maps;
    }

}
